%clear all; clc; 
close all;

%Generates plots using model and data series for Spain 2007-2013

%Updated: 10/26/2022
lambda=0.184;
r=0.02;

%upload model series 
data   =load('model_spain2007-2013.txt');
year =data(:,1);
gn   =data(:,2);
sp   =data(:,3);
gdp  =data(:,4);
ddgdp = gdp(1)-gdp(3);

debt =-data(:,5);
bp   = data(:,6);
unemp=data(:,7);
debt2=-data(:,8);
cons =data(:,9);

%Model 
%mean(gn)=0.22104
%mean(nominal gdp)=4.91718
%mean(real gdp=yT+mean(pn)*yN)=4.9143
%mean(ss gdp)=4.89354
%mean(cons)=0.80304
%mean(ct+mean(pn)*cn)=3.99377

%substract model mean
gn=log(gn);
gn = gn-log(0.22104);
gdp = log(gdp);
gdp = gdp-log(4.89354);
cons = log(cons)-log(0.80304);

%upload data series
datad =load('data_spain2007-2013.txt');
yeard =datad(:,1);
gnd   =datad(:,2);
spd   =datad(:,3);
gdpd  =datad(:,4);
debtd =datad(:,5);
unempd=datad(:,6);
consd=datad(:,7);

%substract mean to data series for 2007-2011
gnd=gnd-mean(gnd(1:5)); 
gnd1=gnd;
gnd=gnd-mean(gnd(1:5)); 
gdpd = gdpd-mean(gdpd(1:5)); 
consd = consd-mean(consd(1:5)); 

NameArray = {'LineStyle','Color','LineWidth'};
ValueArray = {'--','-';[0.23137255012989 0.443137258291245 0.337254911661148],[0.078  0.1686  0.549];1.25,1.25}';

width=6;
height=4;

NameArray = {'LineStyle','Color','LineWidth'};
ValueArray2 = {'--','-';[0.078  0.1686  0.549],[0.8  0  0];1.25,1.25}';
ValueArray0 = {'--';[0.078  0.1686  0.549];1.25}';
ValueArray1 = {'-';[0.8  0  0];1.25}';
left_color =[0.078  0.1686  0.549];
right_color=[0.8  0  0];

nameFig = char(['gn' '_' char(label_model)]);
f=figure('name',nameFig);
set(f,'defaultAxesColorOrder',[left_color;right_color]);
P=plot(year,gnd,year,gn);grid on;
AX1 = gca;box on;%ylabel('g^N');
set(AX1,'YTick',-0.2:0.1:0.1,'XTick',2007:1:2015,'Fontsize',16);
axis(AX1, [year(1) year(end-2) -0.3 0.1]);
set(P,NameArray,ValueArray2);grid on;
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
%saveas(gcf, fullfile(plot_path, nameFig),format_chart);
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);

nameFig = char(['cons' '_' char(label_model)]);
f=figure('name',nameFig);
set(f,'defaultAxesColorOrder',[left_color;right_color]);
P=plot(year,consd,year,cons);
AX1 = gca;grid on;box on;%ylabel('cons');
set(AX1,'YTick',-0.2:0.1:0.1,'XTick',2007:1:2015,'Fontsize',16);
axis(AX1, [year(1) year(end-2) -0.2 0.1]);
set(P,NameArray,ValueArray2);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
%saveas(gcf, fullfile(plot_path, nameFig),format_chart);
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);

nameFig = char(['spread' '_' char(label_model)]);
f=figure('name',nameFig);
P=plot(year,spd,year,sp);grid on;
xlim([year(1) year(end-2)]);

AX1 = gca;box on;ylabel('%','Fontsize',16);
xlim([year(1) year(end-1)]);
set(AX1,'YTick',0:2:8,'XTick',2007:1:2015,'Fontsize',16,'Fontsize',16);
axis(AX1, [year(1) year(end-2) -0.5 8]);
set(P,NameArray,ValueArray2);grid on;
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
%saveas(gcf, fullfile(plot_path, nameFig),format_chart);
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);

nameFig = char(['GDP' '_' char(label_model)]);
f=figure('name',nameFig);
set(f,'defaultAxesColorOrder',[left_color;right_color]);%ylabel('GDP');
P=plot(year,gdpd,year,gdp);
legend('data','model','Location','NorthEast','Fontsize',16);
legend boxoff; 
AX1 = gca;box on;grid on;
set(AX1,'YTick',-0.3:0.1:0.1,'XTick',2007:1:2015,'Fontsize',16);
axis(AX1, [year(1) year(end-2) -0.2 0.1]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
set(P,NameArray,ValueArray2);
%saveas(gcf, fullfile(plot_path, nameFig),format_chart);
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);

nameFig = char(['debt' '_' char(label_model)]);
f=figure('name',nameFig);
P=plot(year,debtd,year,debt);grid on;
ylabel('%','Fontsize',16);
xlim([year(1) year(end-2)]);
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',15:5:35,'XTick',2007:1:2015,'Fontsize',16);
axis(AX1, [year(1) year(end-2) 15 35]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
%saveas(gcf, fullfile(plot_path, nameFig),format_chart);
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);

nameFig = char(['gdp' '_' char(label_model)]);
f=figure('name',nameFig);
P=plot(year,gdpd,year,gdp);grid on;
%ylabel('GDP','Fontsize',16);
xlim([year(1) year(end-2)]);
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',-0.2:0.1:0.1,'XTick',2007:1:2015,'Fontsize',16);
axis(AX1, [year(1) year(end-2) -0.2 0.1]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
print('gdp2_new','-depsc')
%saveas(gcf, fullfile(plot_path, nameFig),format_chart);
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);
%%
nameFig = char(['unemp' '_' char(label_model)]);
f=figure('name',nameFig);
P=plot(year,unempd,year,unemp);
grid on;
ylabel('%','Fontsize',16);
xlim([year(1) year(end-2)]);
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',0:10:30,'XTick',2007:1:2015,'Fontsize',16);
axis(AX1, [year(1) year(end-2) 0 30]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
%saveas(gcf, fullfile(plot_path, nameFig),format_chart);
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);